package exercise.day5;
import java.util.*;

/**
 * @author Dianzhi Han
 * @version 1.0
 * @description: TODO
 * @date 2022/11/3 1:52
 */
public class CM46_LegalParenthesis {
    public boolean chkParenthesis(String A, int n) {
        // write code here
        Deque<Character> stack = new LinkedList<>();
        char[] temp = A.toCharArray();
        char peek;
        for(int i=0;i<n;i++){
            if(temp[i]=='('){
                stack.push(')');
            }else{

                if(stack.isEmpty()){
                    return false;
                }
                peek = stack.peek(); // 判断弹栈空指针 todo
                if(temp[i] == peek){
                    stack.poll();
                }else{
                    return false;
                }
            }
        }


        return stack.isEmpty();
    }
}
